package cn.orangelite.ai.service.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.orangelite.ai.domain.AiConfig;
import cn.orangelite.ai.domain.qianfan.ErnieContentDTO;
import cn.orangelite.ai.domain.qianfan.ErnieBot4Result;
import cn.orangelite.ai.domain.qianfan.StableDiffusionXLDTO;
import cn.orangelite.ai.domain.qianfan.StableDiffusionXLResult;
import cn.orangelite.ai.service.IAiConfigService;
import cn.orangelite.ai.service.IAiInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class AiInfoServiceImpl implements IAiInfoService {

    @Autowired
    private IAiConfigService iAiConfigService;

    @Override
    @Cacheable(value = "qianfan_access_token")
    public String getQianfanToken() {
        AiConfig aiConfig = new AiConfig();
        aiConfig.setName("百度");
        List<AiConfig> aiConfigs = iAiConfigService.selectAiConfigList(aiConfig);

        HashMap<String, Object> param = new HashMap<>();
        param.put("grant_type","client_credentials");
        param.put("client_id",aiConfigs.get(0).getAppId());
        param.put("client_secret",aiConfigs.get(0).getSecretKey());

        HttpResponse execute = HttpRequest.post("https://aip.baidubce.com/oauth/2.0/token").form(param).execute();
        String body = execute.body();

        JSONObject entries = JSONUtil.parseObj(body);

        return entries.getStr("access_token");
    }

    @Override
    public ErnieBot4Result sendContent(String content,String token) {

        HashMap<String, Object> param = new HashMap<>();
        List<ErnieContentDTO> listMessage = new ArrayList<>();
        ErnieContentDTO ernieContentDTO = new ErnieContentDTO();
        ernieContentDTO.setRole("user");
        ernieContentDTO.setContent(content);
        listMessage.add(ernieContentDTO);
        param.put("messages",listMessage);

        System.out.println(JSONUtil.toJsonStr(param));

        HttpResponse execute = HttpRequest.post("https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro?access_token="+token).body(JSONUtil.toJsonStr(param)).contentType("application/json").execute();

        return JSONUtil.toBean(execute.body(), ErnieBot4Result.class);
    }

    @Override
    public StableDiffusionXLResult createImage(String prompt,String token) {
        StableDiffusionXLDTO param = new StableDiffusionXLDTO();
        param.setPrompt(prompt);

        System.out.println(JSONUtil.toJsonStr(param));


        HttpResponse execute = HttpRequest.post(" https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/text2image/sd_xl?access_token="+token).body(JSONUtil.toJsonStr(param)).contentType("application/json").execute();

        return JSONUtil.toBean(execute.body(), StableDiffusionXLResult.class);
    }
}
